<template>
	<view class="loginbox">
		<h1>外卖</h1>
		<u-tabs lineColor="#02a774" :activeStyle="{color:'#02a774'}" :list="tabs" @click="gotab"></u-tabs>
		<view v-show="isActive" class="">
			<input v-model="register.user" type="text" placeholder="请输入注册的用户名" />
			<input v-model="register.pass" type="text" placeholder="请输入注册的密码" />
			<button @click="goRegister">注册</button>
			<p>关于我们</p>
		</view>
		<view v-show="!isActive" class="">
			<input v-model="login.user" type="text" placeholder="请输入登录的用户名" />
			<input v-model="login.pass" type="text" placeholder="请输入登录的密码" />
			<button @click="gologin">登录</button>
			<p>关于我们</p>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 注册
				register: {
					user: '',
					pass: ''
				},
				// 登录
				login: {
					user: '',
					pass: ''
				},
				// 点击tab切换
				isActive: true,
				// tab
				tabs: [{
						name: '注册'
					},
					{
						name: '登录'
					}
				]
			}
		},
		onLoad() {
			const tokenName = uni.getStorageSync('token')
			if (tokenName) {
				uni.switchTab({
					url: '/pages/index/index'
				})
			}
		},
		methods: {
			// 注册
			goRegister() {
				if (!this.register.user) {
					uni.showToast({
						title: '用户名不能为空',
						icon: 'none'
					})
					return
				}
				if (!this.register.pass) {
					uni.showToast({
						title: '密码不能为空',
						icon: 'none'
					})
					return
				}
				uni.request({
					url: `http://10.31.67.9:8002/admin/registeruser?name=${this.register.user}&pass=${this.register.pass}`,
					method: 'GET',
					success: (res) => {
						console.log('注册请求成功', res)
						uni.showToast({
							title: '注册成功',
							icon: 'none'
						})
					},
					fail(err) {
						console.log('注册请求失败', err)
						uni.showToast({
							title: '注册失败',
							icon: 'none'
						})
					}
				})
			},
			// 登录
			gologin() {
				if (!this.login.user) {
					uni.showToast({
						title: '用户名不能为空',
						icon: 'none'
					})
					return
				}
				if (!this.login.pass) {
					uni.showToast({
						title: '密码不能为空',
						icon: 'none'
					})
					return
				}
				uni.request({
					url: 'http://10.31.67.9:8002/admin/loginuser',
					method: 'POST',
					data: {
						name: this.login.user,
						pass: this.login.pass
					},
					success: (res) => {
						console.log('登录请求成功', res)
						if (res.data.msg == 'ok') {
							uni.showToast({
								title: '登录成功',
								icon: 'none'
							})
							uni.setStorageSync('name', res.data.name)
							uni.setStorageSync('token', 'iwjsqiwswhduqwhduwd')
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/index/index'
								})
							}, 1400)
						} else if (res.data.msg == 'userpassiserror') {
							uni.showToast({
								title: '密码错误',
								icon: 'none'
							})
						} else {
							uni.showToast({
								title: '用户名不存在',
								icon: 'none'
							})
						}
					},
					fail(err) {
						console.log('登录请求失败', err)
						uni.showToast({
							title: '登录失败',
							icon: 'none'
						})
					}
				})
			},
			// tab切换
			gotab(item) {
				console.log(item.name, 'item')
				if (item.name == '登录') {
					this.isActive = false
				} else {
					this.isActive = true
				}
			}
		}
	}
</script>

<style lang="scss">
	.loginbox {
		width: 100%;
		height: 100vh;
		background: #fff;
		display: flex;
		flex-direction: column;
		align-items: center;

		h1 {
			margin: 8% 0;
			color: #02a774;
		}

		input {
			width: 240px;
			height: 40px;
			border: 1px solid #ccc;
			margin-top: 10px;
			border-radius: 6px;
			padding-left: 5px;
		}

		button {
			width: 250px;
			height: 40px;
			margin-top: 20px;
			background: #02a774;
			color: #fff;
		}

		p {
			width: 100%;
			text-align: center;
			color: #ccc;
			margin-top: 20px;
		}
	}
</style>